package it.irondev.appturismo.service.impl;

import it.irondev.appturismo.dao.RestaurantsMapper;
import it.irondev.appturismo.dao.RestauranttypeMapper;
import it.irondev.appturismo.dao.example.RestaurantsExample;
import it.irondev.appturismo.dao.example.RestaurantsExample.Criteria;
import it.irondev.appturismo.entity.Attractions;
import it.irondev.appturismo.entity.Restaurant;
import it.irondev.appturismo.entity.Restauranttype;
import it.irondev.appturismo.filter.BaseFilter;
import it.irondev.appturismo.filter.RestaurantFilter;
import it.irondev.appturismo.service.IRestaurantService;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class RestaurantServiceImpl implements IRestaurantService {
	
	
	@Autowired
	RestaurantsMapper restaurantDao;
	
	@Autowired
	RestauranttypeMapper restaurantTypeDao;

	public Restaurant getRestaurantById(Integer idRestaurant) {
		return restaurantDao.selectByPrimaryKey(idRestaurant);
	}

	public void addRestaurant(Restaurant restaurant) {
		restaurantDao.insert(restaurant);
	}

	public List<Restaurant> getAllRestaurants() { 
		return restaurantDao.selectByExample(null);
	}
	
	public List<Restaurant> getAllPremiumRestaurants(Integer idLanguage) {
		RestaurantsExample example = new RestaurantsExample();
		Criteria crit = example.createCriteria();
		crit.andPremiumEqualTo(1);
		crit.andLanguageEqualTo(idLanguage);
		example.or(crit);
		return restaurantDao.selectByExample(example);
	}

	

	public void updateRestaurant(Restaurant restaurant) {
		restaurantDao.updateByPrimaryKeySelective(restaurant);
	}

	public void deleteRestaurant(Integer idRestaurant) {
		restaurantDao.deleteByPrimaryKey(idRestaurant);
	}

	public List<Restauranttype> getAllRestaurantTypes() {
		return restaurantTypeDao.selectByExample(null);
	}

	public List<Restaurant> getPremiumRestaurantsByType(Integer idLanguage,
			Integer type) {
		RestaurantsExample example = new RestaurantsExample();
		Criteria crit = example.createCriteria();
		crit.andPremiumEqualTo(1);
		crit.andLanguageEqualTo(idLanguage);
		crit.andTypeEqualTo(type);
		example.or(crit);
		return restaurantDao.selectByExample(example);		
	}

	public Restauranttype getRestaurantTypeBySigla(String sigla) {
		return restaurantTypeDao.selectByAbbreviation(sigla);
	}

	public List<Restaurant> getRestaurantsByFilter(final BaseFilter filter) {
		RestaurantsExample example = null;
		RestaurantFilter rFilter = (RestaurantFilter) filter;
		if(rFilter!=null) {
			example = new RestaurantsExample();
			Criteria criteria = example.createCriteria();
			if(rFilter.getId()!=null)
				criteria.andIdrestaurantEqualTo(filter.getId());
			if(rFilter.getIdlanguage()!=null)
				criteria.andLanguageEqualTo(filter.getIdlanguage());
			if(rFilter.getPremium()!=null) 
				criteria.andPremiumEqualTo(rFilter.getPremium().booleanValue() ? 1 : 0);
			if(rFilter.getType()!=null) 
				criteria.andTypeEqualTo(rFilter.getType());
			example.or(criteria);
		}
		List<Restaurant> restaurants = restaurantDao.selectByExample(example);
		
		if(filter!=null && filter.getFavouriteCity()!=null)
			Collections.sort(restaurants, new Comparator<Restaurant>() {
				public int compare(Restaurant o1, Restaurant o2) {
					if(o1.getCity().getIdcity().equals(filter.getFavouriteCity()))
						return -1;
					else return 1;
				}
				
			});
		return restaurants;
	}

}
